import 'babel-polyfill'
import 'classlist-polyfill'
import Vue from 'vue'
import axios from './router/axios'
import VueAxios from 'vue-axios'
import App from './App'
import './permission' // 权限
// import './error' // 日志
import router from './router/router'
import store from './store'
import { loadStyle } from './util/util'
import * as urls from '@/config/env'
import { iconfontUrl, iconfontVersion } from '@/config/env'
import * as filters from './filters' // 全局filter
import ElementUI from 'element-ui'
// import 'element-ui/lib/theme-chalk/index.css'
// import iView from 'iview'
// import 'iview/dist/styles/iview.css'
import './styles/common.scss'
import AvueFormDesign from '@sscfaith/avue-form-design'
import basicContainer from './components/basic-container/main'
import $ from 'jquery'
window.$ = $
export default $

import base from "@/base"
import elementx from "@/global/elementx"
Vue.prototype.elementx = elementx;
Vue.use(base);
import JQUI from './global/jquery-ui.min'
import './global/css/jquery-ui.min.css'


// 插件 json 展示
Vue.use(router)

Vue.use(AvueFormDesign);

Vue.use(VueAxios, axios)

Vue.use(ElementUI, {
  size: 'small',
  menuType: 'text'
})

Vue.use(window.AVUE, {
  size: 'small',
  menuType: 'text'
})

// Vue.use(iView)


// 注册全局容器
Vue.component('basicContainer', basicContainer)

// 加载相关url地址
Object.keys(urls).forEach(key => {
  Vue.prototype[key] = urls[key]
})

// 加载过滤器
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

// 动态加载阿里云字体库
iconfontVersion.forEach(ele => {
  loadStyle(iconfontUrl.replace('$key', ele))
})

Vue.config.productionTip = false

// new Vue({
//   router,
//   store,
//   render: h => h(App)
// }).$mount('#app')

var app;
export function createApp() {
  app = new Vue({
    router,
    store,
    render: h => h(App),
    methods:{
      callJs_1 : function(action,paramers){
        this.callJs_(action,paramers);
      },
      drag_ : function(event){
        this.drag(event);
      },
      dropAfter_ : function(event){
        this.dropAfter(event);
      },
      drop_ : function(event){
        this.drop(event);
      },
      allowDrop_ : function(event){
        this.allowDrop(event);
      },
      getFocus_ : function(event){
        this.getFocus(event);
      },
      getFocusCustomer_ : function(event){
        this.getFocusCustomer(event);
      }
    }
  })
  return {
    app,
    router,
    store
  }
}


// 非Vue全局函数定义 开始
window.callJavaNative = function(action,paramers){
  if(app.isIE_() || app.isIE11_()){  // 判断是否是IE
    callMyJava(action,paramers);
  }else{
    console.log("{\"action\":\""+action+"\","+paramers.substring(1));
    var request_id = window.cefQuery({
      request : "{\"action\":\""+action+"\","+paramers.substring(1),
      persistent : false,
      onSuccess : function(response) {
        console.log(" call java success");
      },
      onFailure : function(error_code, error_message) {
        console.log(" call java faile");
      }
    });
  }

}

window.callJs = function(action,paramers){
  app.callJs_1(action,paramers);
}

window.setDraggable = function(event,flag){
  event.currentTarget.setAttribute("draggable",flag);
}

$().ready(function(){
  //fixed container
  // drop
  $('body').on('drop', '#pageinitccontainer .yfcontainer', function(event){
    event.stopPropagation();
    setDraggable(event,false);
    app.drop_(event);
  });
  //dragover
  $('body').on('dragover', '#pageinitccontainer .yfcontainer', function(event){
    event.stopPropagation();
    setDraggable(event,false);
    app.allowDrop_(event);
  });
  //click
  $('body').on('click', '#pageinitccontainer .yfcontainer', function(event){
    event.stopPropagation();
    setDraggable(event,false);
    app.getFocusCustomer_(event);
  });


  //basic container
  //drop
  $('body').on('drop', '#pageinitccontainer .ycontainer', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.drop_(event);
  });
  //dragover
  $('body').on('dragover', '#pageinitccontainer .ycontainer', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.allowDrop_(event);
  });
  //click
  $('body').on('click', '#pageinitccontainer .ycontainer', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.getFocusCustomer_(event);
  });


  //basic yelement
  //mouseenter
  $('body').on('mouseenter', '#pageinitccontainer .yelement', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.getFocus_(event);
  });
  //dragstart
  $('body').on('dragstart', '#pageinitccontainer .yelement', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.drag_(event);
  });
  //drop
  $('body').on('drop', '#pageinitccontainer .yelement', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.dropAfter_(event);
  });
  //click
  $('body').on('click', '#pageinitccontainer .yelement', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.getFocus_(event);
  });

  //yelementcontainer
  //mouseenter
  $('body').on('mouseenter', '#pageinitccontainer .yelementcontainer', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.getFocus_(event);
  });
  //drop
  $('body').on('drop', '#pageinitccontainer .yelementcontainer', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.drop_(event);
  });
  //dragover
  $('body').on('dragover', '#pageinitccontainer .yelementcontainer', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.allowDrop_(event);
  });
  //click
  $('body').on('click', '#pageinitccontainer .yelementcontainer', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.getFocusCustomer_(event);
  });

  //pageinitccontainer
  // drop
  $('body').on('drop', '#pageinitccontainer', function(event){
    event.stopPropagation();
    setDraggable(event,false);
    app.drop_(event);
  });
  //dragover
  $('body').on('dragover', '#pageinitccontainer', function(event){
    event.stopPropagation();
    setDraggable(event,false);
    app.allowDrop_(event);
  });
  //click
  $('body').on('click', '#pageinitccontainer', function(event){
    event.stopPropagation();
    setDraggable(event,false);
    app.getFocusCustomer_(event);
  });


  ///////////////----------------------------!!!!!
  //mouseenter
  $('body').on('mouseenter', '#pageinitccontainer .is-leaf', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.getFocus_(event);
  });
  //click
  $('body').on('click', '#pageinitccontainer .is-leaf', function(event){
    event.stopPropagation();
    setDraggable(event,true);
    app.getFocus_(event);
  });

  //针对 el-select click 做特殊处理
  $(".el-select").on("click",".el-input__inner",function(event){
    event.stopPropagation();
    event.currentTarget = event.currentTarget.parentNode.parentNode;
    console.log(event.currentTarget);
    app.getFocus_(event);
  });

  if (document.getElementById("pageinitccontainer")){
    //listener mouse resizable
    $(document).mouseup(function(event){
      var classname = event.target.offsetParent.getAttribute("class");
      if(classname != null && classname.indexOf("ui-resizable") != -1){
        var obj = {};
        obj.id = event.target.offsetParent.getAttribute("id");
        if(obj.id == null || obj.id == "null") return;
        var style_ = event.target.offsetParent;
        style_.style.border = "";
        style_.style.padding = "";
        style_.style.transform = "";
        obj.style = style_.getAttribute("style");
        console.log("resizable:"+JSON.stringify(obj));
        callJavaNative("resizable",JSON.stringify(obj));
      }else{  // recall resizable
        try{
          var obj = {};
          obj.id = app.elementx.elementSelectedDom.getAttribute("id");
          if(obj.id == null || obj.id == "null") return;
          var style_ = app.elementx.elementSelectedDom;
          style_.style.border = "";
          style_.style.padding = "";
          style_.style.transform = "";
          obj.style = style_.getAttribute("style");
          console.log("resizable(else):"+JSON.stringify(obj));
          callJavaNative("resizable",JSON.stringify(obj));
        }catch(e){
          console.log(e);
        }

      }
    });

    //listener  ctrl + s
    $(document).keydown(function(e) {
      if (e.keyCode == 83 && e.ctrlKey) {
        callJavaNative("save","{}");
      }
    });

    //listener  ctrl + z undo
    $(document).keydown(function(e) {
      if (e.keyCode == 90 && e.ctrlKey) {
        callJavaNative("undo","{}");
      }
    });

    //listener  ctrl + y  redo
    $(document).keydown(function(e) {
      if (e.keyCode == 89 && e.ctrlKey) {
        callJavaNative("redo","{}");
      }
    });
  }
});

//javascript listener ctrl+onmousewheel ------- begin
var scrollFunc = function(e){
  e=e || window.event;
  if(e.wheelDelta && event.ctrlKey){//IE/Opera/Chrome
    if(e.wheelDelta < 0){  //zoomOut
      callJavaNative("zoomOut","{}");
    }else if(e.wheelDelta > 0){ //zoomIn
      callJavaNative("zoomIn","{}");
    }
  }else if(e.detail){//Firefox
    // the ide exclude firefox brower, therefore do nothing
    event.returnValue=false;
  }
}
/*注册事件*/
if(document.addEventListener){
  document.addEventListener('DOMMouseScroll',scrollFunc,false);
}
//W3C
window.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome/Safari
//javascrapt listener ctrl+onmousewheel ------- end
//非Vue全局函数定义  结束
